package com.atguigu.gulimall.product.web;


import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping({"/","/index","/index.html"})
    public String indexPage(Model model){
      List<CategoryEntity> categoryEntities =  categoryService.getLevel1Categorys();

        model.addAttribute("categorys",categoryEntities);
        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String,List<Catelog2Vo>> getCatalogJson(){

        Map<String ,List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        RLock lock = redissonClient.getLock("my-lock");
        lock.lock();//自动给锁续期，执行业务后才会释放锁
        lock.lock(10, TimeUnit.SECONDS);//不会自动给锁续期，时间一到，就释放锁

        try{
            System.out.println("执行业务代码-----"+Thread.currentThread().getId());
            Thread.sleep(5000);
        }catch (Exception e){

        }finally {
            System.out.println("解锁成功"+Thread.currentThread().getId());

                lock.unlock();

        }

        return "hello";
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/write")
    @ResponseBody
    public String write(){
        RReadWriteLock write = redissonClient.getReadWriteLock("write");
        RLock rLock = write.writeLock();
        rLock.lock();
        String s="";
        try{

            System.out.println("执行业务代码-----");
            s= UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("writeValue",s);
            Thread.sleep(15000);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String read(){
        RReadWriteLock write = redissonClient.getReadWriteLock("write");
        RLock rLock = write.readLock();
        rLock.lock();
        String  s = "";

        try {
            s  = stringRedisTemplate.opsForValue().get("writeValue");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }

        return s;
    }

    /**
     * 信号量也可以做限流
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");

        park.acquire();
        //park.tryAcquire();

        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");

        park.release();

        return "ok";
    }


    @GetMapping("/close")
    @ResponseBody
    public String close() throws InterruptedException {
        RCountDownLatch close = redissonClient.getCountDownLatch("close");
        close.trySetCount(5);
        close.await();
        return "关门了";
    }

    @GetMapping("/gogo/{id}")
    @ResponseBody
    public String close(@PathVariable("id") String id) throws InterruptedException {
        RCountDownLatch close = redissonClient.getCountDownLatch("close");
        close.countDown();
        return id+"班关门了";
    }

}
